<?php

namespace app\common\model;

class Site extends Base
{
    protected $name = 'site_config';
    // 追加属性
    protected $append = [
    ];

    const TYPE_STRING   = 'string';     // 字符串
    const TYPE_TEXT     = 'text';       // 长字符串
    const TYPE_EDITOR   = 'editor';     // 富文本
    const TYPE_NUMBER   = 'number';     // 数字类型
    const TYPE_DATE     = 'date';       // 日期类型
    const TYPE_TIME     = 'time';       // 时间戮
    const TYPE_DATETIME = 'datetime';   // 日期类型
    const TYPE_SELECT   = 'select';     // 下拉框
    const TYPE_SELECTS  = 'selects';    // 列表(多选)
    const TYPE_IMAGE    = 'image';      // 图片
    const TYPE_IMAGES   = 'images';     // 图片列表
    const TYPE_FILE     = 'file';       // 文件类型
    const TYPE_FILES    = 'files';      // 多文件类型
    const TYPE_SWITCH   = 'switch';     // 开关
    const TYPE_CHECKBOX = 'checkbox';   // 多选框按钮
    const TYPE_RADIO    = 'radio';      // 单选框按钮
    const TYPE_ARRAY    = 'array';      // 数组类型
    const TYPE_CURTOM   = 'custom';
    const TYPE_MAP      = 'map';        // 地图
    /**
     * 读取配置类型
     * @return array
     */
    public static function getTypeList()
    {
        $typeList = [
            'string'   => __('String'),
            'text'     => __('Text'),
            'editor'   => __('Editor'),
            'number'   => __('Number'),
            'date'     => __('Date'),
            'time'     => __('Time'),
            'datetime' => __('Datetime'),
            'select'   => __('Select'),
            'selects'  => __('Selects'),
            'image'    => __('Image'),
            'images'   => __('Images'),
            'file'     => __('File'),
            'files'    => __('Files'),
            'switch'   => __('Switch'),
            'checkbox' => __('Checkbox'),
            'radio'    => __('Radio'),
            'array'    => __('Array'),
            'custom'   => __('Custom'),
            'map'      => __('Map'),
        ];
        return $typeList;
    }

    public static function getRegexList()
    {
        $regexList = [
            'required' => '必选',
            'digits'   => '数字',
            'letters'  => '字母',
            'date'     => '日期',
            'time'     => '时间',
            'email'    => '邮箱',
            'url'      => '网址',
            'qq'       => 'QQ号',
            'IDcard'   => '身份证',
            'tel'      => '座机电话',
            'mobile'   => '手机号',
            'zipcode'  => '邮编',
            'chinese'  => '中文',
            'username' => '用户名',
            'password' => '密码'
        ];
        return $regexList;
    }

    public static function getArrayData($data)
    {
        if (!isset($data['value'])) {
            $result = [];
            foreach ($data as $index => $datum) {
                $result['field'][$index] = $datum['key'];
                $result['value'][$index] = $datum['value'];
            }
            $data = $result;
        }
        $fieldarr = $valuearr = [];
        $field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []);
        $value = isset($data['value']) ? $data['value'] : [];
        foreach ($field as $m => $n) {
            if ($n != '') {
                $fieldarr[] = $field[$m];
                $valuearr[] = $value[$m];
            }
        }
        return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
    }

    /**
     * 将字符串解析成键值数组
     * @param string $text
     * @return array
     */
    public static function decode($text, $split = "\r\n")
    {
        $content = explode($split, $text);
        $arr = [];
        foreach ($content as $k => $v) {
            if (stripos($v, "|") !== false) {
                $item = explode('|', $v);
                $arr[$item[0]] = $item[1];
            }
        }
        return $arr;
    }

    /**
     * 将键值数组转换为字符串
     * @param array $array
     * @return string
     */
    public static function encode($array, $split = "\r\n")
    {
        $content = '';
        if ($array && is_array($array)) {
            $arr = [];
            foreach ($array as $k => $v) {
                $arr[] = "{$k}|{$v}";
            }
            $content = implode($split, $arr);
        }
        return $content;
    }

    public static function getDefaultConfig()
    {
        return [
            [ 'name' => 'name', 'title' => '公司名称', 'tip' => '', 'type' => self::TYPE_STRING, 'value' => "", 'content' => '', 'rule' => 'required', 'extend' => ''],
            [ 'name' => 'logo', 'title' => '公司logo', 'tip' => '', 'type' => self::TYPE_IMAGE, 'value' => "", 'content' => '', 'rule' => 'required', 'extend' => ''],
        ];
    }

    public function getConfig($name = '', $field = true, $default = '')
    {
        if ($field === true) {
            $value = $this->where('name', $name)->find();
            if (!$value && !empty($default)) {
                $value = $default;
            }
        } else {
            $value = $this->where('name', $name)->value($field);
            if (is_null($value)) {
                $value = $default;
            }
        }

        return $value;
    }

}